﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Reflection;
using System.Data;
using DAL;

namespace UtilLibrary
{
    public class DataAccessor
    {
        public static void InsertData(BaseBizEntity pBizEntity)
        {
            String fullName = pBizEntity.GetDataEntityType().FullName;
            EntityObject entity = (EntityObject)pBizEntity.GetDataEntityType().Assembly.CreateInstance(fullName);
            DbUtil.BizObjTransToDataEntity(pBizEntity, entity);

            Model1Container context = new Model1Container();
            string[] names = entity.ToString().Split('.');
            context.AddObject(names[names.Length - 1], entity);
            int row = context.SaveChanges();
            pBizEntity.Id = Convert.ToInt32(entity.EntityKey.EntityKeyValues.First().Value);
        }

        public static EntityObject UpdateData(BaseBizEntity pBizEntity)
        {
            Model1Container context = new Model1Container();

            IEnumerable<KeyValuePair<string, object>> entityKeyValues = new KeyValuePair<string, object>[] 
            {
                new KeyValuePair<string, object>("Id", pBizEntity.Id) 
            };
            
            //EntityKey key = context.CreateEntityKey("Model1.task", pEntity);
            EntityKey key = new EntityKey(pBizEntity.EntitySetName, entityKeyValues);
            //EntityKey key = new EntityKey("Model1Container.task", names[names.Length - 1], pBizEntity.Id);
            EntityObject entity = (EntityObject)context.GetObjectByKey(key);
            DbUtil.BizObjTransToDataEntity(pBizEntity, entity);
            context.SaveChanges();
            return entity; 
        }

        public static void DeleteData(BaseBizEntity pBizEntity)
        {
            IEnumerable<KeyValuePair<string, object>> entityKeyValues = new KeyValuePair<string, object>[] 
            {
                new KeyValuePair<string, object>("Id", pBizEntity.Id)
            };

            Model1Container context = new Model1Container();
            EntityKey key = new EntityKey(pBizEntity.EntitySetName, entityKeyValues);

            EntityObject obj =(EntityObject)context.GetObjectByKey(key);

            context.DeleteObject(obj);
            context.SaveChanges();
        }

        public static BaseBizEntity GetBizDataByID(int pID, Type pBaseBizType)
        {
            BaseBizEntity ret = (BaseBizEntity)pBaseBizType.Assembly.CreateInstance(pBaseBizType.FullName);
            IEnumerable<KeyValuePair<string, object>> entityKeyValues = new KeyValuePair<string, object>[] 
            {
                new KeyValuePair<string, object>("Id", pID) 
            };
            Model1Container context = new Model1Container();
            EntityKey key = new EntityKey(ret.EntitySetName, entityKeyValues);
            Object o = new object();
            context.TryGetObjectByKey(key, out o);
            if (o != null)
            {
                DbUtil.DataEntityToBizObjTrans((EntityObject)o, ret);
            }
            else ret = null;

            return ret;
        }

        public static EntityObject GetEntityByID(int pID, Type pEntityType)
        {
             EntityObject ret = null;
            IEnumerable<KeyValuePair<string, object>> entityKeyValues = new KeyValuePair<string, object>[] 
            {
                new KeyValuePair<string, object>("Id", pID) 
            };
            
            Model1Container context = new Model1Container();
            EntityKey key = new EntityKey(GetEntitySetName(pEntityType), entityKeyValues);
            object obj = context.GetObjectByKey(key);
            ret = obj == null ? null : (EntityObject)obj;

            return ret;
        }

        private static string GetEntitySetName(Type pType)
        {
            if (pType == null) return String.Empty;

            string[] names = pType.GetType().ToString().Split('.');
            return DbUtil.ConcatEntityNameSet(names[names.Length - 1]);
        }
    }
}
